﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using myomscl.Utils;
using omsData;

namespace Workflow.Layout.Layouts
{
    public class BaseLayout : System.Web.UI.Page
    {
        private const string SearchCriteria = "SearchCriteria";
        protected User user;


        #region Protected Methods
        protected string GetSearchText()
        {
            if (Utils.Context.Item != "Search")
            {
                Session[SearchCriteria] = null;
                return string.Empty;
            }
            else
            {
                return Session[SearchCriteria].ToString();
            }
        }

        protected void SetResponseCacheAttributes()
        {
            Response.AppendHeader("Pragma", "no-cache");
            Response.AppendHeader("Cache-Control", "no-cache");
            Response.CacheControl = "no-cache";
            Response.Expires = -1;
            Response.ExpiresAbsolute = new DateTime(1900, 1, 1);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }

        protected void NavigateToStartPage()
        {
            //if (Utils.Context.Item.Name == "workflow")
            //{
            //    if (user.Type == WorkflowEnums.UserType.Municipality)
            //    {
            //        Response.Redirect(String.Format("~/{0}/Alerts/NewAlerts.aspx", user.Name));
            //    }
            //    else if (user.Type == WorkflowEnums.UserType.NetworkOperator)
            //    {
            //        Response.Redirect(String.Format("~/{0}/Alerts/NewAlerts.aspx", "NetworkOperator"));
            //    }
            //}

            using (OmsModelContainer context = new OmsModelContainer())
            {
                var _user = context.Users.Where(u => u.Id == user.Id).First();
                user.OrganisationReference.Load();
                var _organization = user.Organisation;
                _organization.UserTypeReference.Load();
                var _userType = _organization.UserType;

                string navigateList = _userType.ListScreens.First().Name;

                Response.Redirect(string.Format("~/{0}/{1}", _organization.Name, navigateList));
            }
        }

        protected void Logout()
        {
            Session["logout"] = "1";
            Session[Constants.CurrentUser] = null;
            Response.Redirect("~/login.aspx");
        }

        protected void Search(string searchText)
        {
            Session[SearchCriteria] = searchText;

            //String query = String.Empty;
            //switch (user.Type)
            //{
            //    case WorkflowEnums.UserType.Municipality:
            //        query = String.Format("/sitecore/content/workflow/{0}//*[@@templatename='SearchPage']", user.Name);
            //        break;
            //    case WorkflowEnums.UserType.NetworkOperator:
            //        query = "/sitecore/content/workflow/NetworkOperator//*[@@templatename='SearchPage']";
            //        break;
            //}
            //var searchUrl = Sitecore.Context.Item.Axes.SelectSingleItem(query);
            //Response.Redirect(Sitecore.Links.LinkManager.GetItemUrl(searchUrl));
        }

        //protected virtual void SetTitle()
        //{
        //    this.Page.Title = "Workflow";
        //}

        protected void SetNavigationLinks(System.Web.UI.WebControls.HyperLink lnkSettings,
                                          System.Web.UI.WebControls.HyperLink lnkProfile,
                                          System.Web.UI.WebControls.HyperLink lnkHelp,
                                          System.Web.UI.WebControls.HyperLink lnkLogo)
        {
            //if (user.Type == WorkflowEnums.UserType.NetworkOperator)
            //{
            //    lnkSettings.NavigateUrl = String.Format("~/{0}/Alerts/Settings/Network Operator Details.aspx", "NetworkOperator");
            //    lnkProfile.NavigateUrl = String.Format("~/{0}/Alerts/Settings/MyProfile.aspx", "NetworkOperator");
            //    lnkHelp.NavigateUrl = String.Format("~/{0}/Alerts/help/newalert.aspx", "NetworkOperator");
            //    lnkLogo.NavigateUrl = String.Format("~/{0}/Alerts/NewAlerts.aspx", "NetworkOperator");

            //}
            //else
            //{
            //    lnkSettings.NavigateUrl = string.Format("~/{0}/Alerts/Settings/Network operators.aspx", user.Name);
            //    lnkProfile.NavigateUrl = string.Format("~/{0}/Alerts/Settings/MyProfile.aspx", user.Name);
            //    lnkHelp.NavigateUrl = string.Format("~/{0}/Alerts/help/alerts.aspx", user.Name);
            //    lnkLogo.NavigateUrl = String.Format("~/{0}/Alerts/NewAlerts.aspx", user.Name);

            //}
        }

        //protected virtual void SetMenuSources() { }


        #endregion

        #region Properties
        protected string MenuSource
        {
            get
            {
                //switch (user.Type)
                //{
                //    case WorkflowEnums.UserType.Municipality:
                //        return string.Format("/sitecore/content/workflow/{0}/Alerts/AlertSettings/Navigation/MainNavigation", user.Name);
                //    case WorkflowEnums.UserType.NetworkOperator:
                //        return "/sitecore/content/workflow/NetworkOperator/Alerts/AlertSettings/Navigation/MainNavigation";
                //}
                return string.Empty;
            }
        }

        protected string SubMenuSource
        {
            get
            {
                //switch (user.Type)
                //{
                //    case WorkflowEnums.UserType.Municipality:
                //        if (Sitecore.Context.Item.TemplateName == "Helppage")
                //            return string.Format("/sitecore/content/workflow/{0}/Alerts/Alertsettings/Navigation/HelpMenu", user.Name);
                //        else
                //            return string.Format("/sitecore/content/workflow/{0}/Alerts/Alertsettings/Navigation/SettingsMenu", user.Name);
                //    case WorkflowEnums.UserType.NetworkOperator:
                //        if (Sitecore.Context.Item.TemplateName == "Helppage")
                //            return "/sitecore/content/workflow/NetworkOperator/Alerts/Alertsettings/Navigation/HelpMenu";
                //        else

                //            return "/sitecore/content/workflow/NetworkOperator/Alerts/Alertsettings/Navigation/SettingsMenu";
                //}
                return string.Empty;
            }
        }

        protected string BranchName
        {
            get
            {

                //return (user.Type == WorkflowEnums.UserType.NetworkOperator) ? "/NetworkOperator/" :
                //                                                             string.Format("/{0}/", user.Name);
                return string.Empty;
            }
        }

        #endregion

        #region Events
        protected virtual void Page_Load(object sender, EventArgs e)
        {
            user = Session[Constants.CurrentUser] as User;

            if (user == null || Utils.Context.Item == null)
            {
                Response.Redirect("~/login.aspx");
            }

            SetResponseCacheAttributes();
            NavigateToStartPage();
        }
        #endregion
    }
}
